#import "TDMineBasicInfoC.h"
#import "TBRMineSettingIconCell.h"
#import "TBRMineSettingBasicCell.h"
#import "TBRMineModel.h"
#import <YYModel.h>
#import <UIImageView+WebCache.h>
#import "PhotoBaseManager.h"
#import "TBRLoginController.h"
#import "BRPickerView.h"

#import "TBRNameSettingC.h"
#import "TDMinePhoneSettingC.h"
#import "NetWorkRequest.h"

#import "TDIdCardMessageGetC.h"

@interface TDMineBasicInfoC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *localPhotoArray;//图片数组-------
@property (nonatomic,strong)NSMutableArray *urlPhotoArray;//图片链接数组-------------
@property (nonatomic,strong)NSArray *payTypeArray;//支付数组------
@end

@implementation TDMineBasicInfoC

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载数据
    [self loadData];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
}

- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    //添加tableView
    [self setupTableView];
}


- (void)setupTableView {
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRMineSettingIconCell" bundle:nil] forCellReuseIdentifier:@"TBRMineSettingIconCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRMineSettingBasicCell" bundle:nil] forCellReuseIdentifier:@"TBRMineSettingBasicCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -45, 0, 0)];
    }
    self.tableView.showsVerticalScrollIndicator = NO;
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *tmpArray = self.dataArray[section];
    return tmpArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TBRMineSettingIconCellId";
    if (indexPath.section == 0 && indexPath.row == 0) {
        iden = @"TBRMineSettingIconCellId";
    }else {
        iden = @"TBRMineSettingBasicCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        TBRMineSettingIconCell *iconCell = (TBRMineSettingIconCell *)cell;
        //取出本地图片数组和网络图片数组
        NSString *urlStr = [NSString stringWithFormat:@"http://121.40.92.131/deepdriving/%@",[AppSetting shareAppSetting].photo];
        [iconCell.imv_icon sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"头像"]];
    }else {
        TBRMineSettingBasicCell *infoBasicCell = (TBRMineSettingBasicCell *)cell;
        NSArray *tmpArray = self.dataArray[indexPath.section];
        TBRMineInfoBasicModel *basicModel = tmpArray[indexPath.row];
        infoBasicCell.basicModel = basicModel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 && indexPath.row == 0 ? 85 : 69;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        self.localPhotoArray = [NSMutableArray array];
        [self showAlert];
    }
    else {
        __weak typeof(self) weakSelf = self;
        NSArray *tmpArray = self.dataArray[indexPath.section];
        TBRMineInfoBasicModel *basicModel = tmpArray[indexPath.row];
        if ([basicModel.messageId isEqualToString:@"idCheck"]) {
            TDIdCardMessageGetC *idCardMessageGetC = [[TDIdCardMessageGetC alloc] init];
            [self.navigationController pushViewController:idCardMessageGetC animated:YES];
            return;
        }
        else if ([basicModel.messageId isEqualToString:@"userName"]) {
            TBRNameSettingC *nameSettingC = [[TBRNameSettingC alloc] init];
            nameSettingC.nameSettingCBlock = ^(NSString *name) {
                basicModel.detailName = name;
                [weakSelf.tableView reloadData];
            };
            [self.navigationController pushViewController:nameSettingC animated:YES];
        }else if ([basicModel.messageId isEqualToString:@"phoneNum"]) {
            TDMinePhoneSettingC *phoneSettingC = [[TDMinePhoneSettingC alloc] init];
            [self.navigationController pushViewController:phoneSettingC animated:YES];
        }else if ([basicModel.messageId isEqualToString:@"password"]) {
            [BRStringPickerView showStringPickerWithTitle:@"请选择支付类型" dataSource:_payTypeArray defaultSelValue:basicModel.detailName resultBlock:^(id selectValue) {
                basicModel.detailName = selectValue;
                [weakSelf.tableView reloadData];
            }];
        }
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.dataArray = [NSMutableArray array];
    self.localPhotoArray = [NSMutableArray array];
    self.urlPhotoArray = [NSMutableArray array];
    NSArray *tmpArray = @[
                          @[@{@"messageId":@"userName",@"imageName":@"",@"titleName":@"昵称",
                              @"detailName":@"丁晓龙"},
                            @{@"messageId":@"userName",@"imageName":@"",@"titleName":@"昵称",
                              @"detailName":[HelpObject changeNull:[AppSetting shareAppSetting].nickname]},
                            @{@"messageId":@"idCheck",@"imageName":@"",@"titleName":@"身份认证",@"detailName":@"未认证"}],
                          @[@{@"messageId":@"phoneNum",@"imageName":@"",@"titleName":@"手机号",@"detailName":[HelpObject changeNull:_shareMineInfoModel.phone]}],
                          @[@{@"messageId":@"password",@"imageName":@"",@"titleName":@"修改密码",@"detailName":@"未设置"},
                            ]
                          ];
    
    for (NSArray *tmpEnumArray in tmpArray) {
        NSArray *tmpModelArray = [NSArray yy_modelArrayWithClass:[TBRMineInfoBasicModel class] json:tmpEnumArray];
        [self.dataArray addObject:tmpModelArray];
    }
    self.payTypeArray = @[@"预付卡扣款",@"月付卡支付"];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}

// MARK: - 上传图片数据
- (void)uploadIconImg {
    [self progressHUDShowWithText:nil];
    [self.urlPhotoArray removeAllObjects];
//    UIImage *image = self.localPhotoArray.firstObject;
//    image = [NetWorkRequest fixOrientation:image];
    NSData *imageData = self.localPhotoArray.firstObject;
    NSString *pictureDataString = [imageData base64EncodedStringWithOptions:0];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{@"oldupdatetime":[HelpObject changeNull:[AppSetting shareAppSetting].updatetime],
                          @"updateuserid":[HelpObject changeNull:[AppSetting shareAppSetting].memberid],
                          @"imagetype":@"6",
                          @"imagedata":pictureDataString
                          };
    [NetWorkRequest postNetRequestData:UpLoadMemberMessage parmeters:dic response:^(NSDictionary *response) {
        [weakSelf progressHUDHideImmediately];
        if (response) {
            [AppSetting shareAppSetting].updatetime = response[@"data"][@"updatetime"];
            [AppSetting shareAppSetting].photo = response[@"data"][@"imageurl"];
            [weakSelf.tableView reloadData];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"头像上传失败" isSucceed:NO];
    }];
}



- (void)showAlert {
    //1.弹出窗口
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请选择类型" preferredStyle:UIAlertControllerStyleActionSheet];
    //     UIAlertControllerStyleActionSheet
    //1.1添加确定动作
    UIAlertAction *caramaAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击后进行的操作----跳转到相机界面
        
        //根据有无图片来判断是拍照还是大图查看
        __weak typeof(self) weakSelf = self;
        [[PhotoBaseManager photoBaseShareManage] getPhotoPicture:self compress:YES photoSize:100 photoWidth:500 edited:NO photoBlock:^(NSData *imageData) {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf.localPhotoArray addObject:imageData];
            [weakSelf uploadIconImg];
        }];
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册获取
        __weak typeof(self) weakSelf = self;
        [[PhotoBaseManager photoBaseShareManage] getPhotoPictureFromAlbum:self compress:YES photoSize:100 photoWidth:500 edited:NO photoBlock:^(NSData *imageData) {
            //            [model.localPhotos addObject:imageData];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf.localPhotoArray addObject:imageData];
            [weakSelf uploadIconImg];
        }];
    }];
    [alertController addAction:caramaAction];
    [alertController addAction:photoAction];
    //1.2添加取消动作
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
@end
